package com.xpj.callable;

import java.util.concurrent.Callable;

public class MyFutureTask1<V> implements Runnable{

    private Callable<V> callable;
    private V v;
    private volatile Thread runningThread;

    public MyFutureTask1(Callable<V> callable){
        this.callable = callable;
    }

    @Override
    public void run() {
        try {
            runningThread = Thread.currentThread();
            this.v = callable.call();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public V get(){
        boolean flag = true;
        while(flag) {
            if (runningThread != null) {
                flag = false;
                try {
                    runningThread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
        return v;
    }
}
